<template>
  <div class="login-page">
    <!-- 背景装饰 -->
    <div class="background-decoration">
      <div class="circle circle-1"></div>
      <div class="circle circle-2"></div>
      <div class="circle circle-3"></div>
    </div>

    <!-- 登录容器 -->
    <div class="login-container">
      <div class="login-card">
        <!-- 头部 -->
        <div class="login-header">
          <h1 class="login-title">欢迎回来</h1>
          <p class="login-subtitle">请登录您的账户</p>
        </div>

        <!-- 登录表单 -->
        <el-form ref="ruleFormRef" class="login-form" :model="ruleForm" :rules="rules" label-width="0">
          <el-form-item prop="userName">
            <div class="input-group">
              <el-icon class="input-icon">
                <User />
              </el-icon>
              <el-input v-model="ruleForm.userName" placeholder="请输入用户名" class="custom-input" size="large" />
            </div>
          </el-form-item>

          <el-form-item prop="password">
            <div class="input-group">
              <el-icon class="input-icon">
                <Lock />
              </el-icon>
              <el-input v-model="ruleForm.password" type="password" placeholder="请输入密码" class="custom-input"
                size="large" show-password />
            </div>
          </el-form-item>

          <div class="form-options">
            <el-checkbox v-model="ruleForm.regin" class="remember-checkbox">
              记住密码
            </el-checkbox>
            <a href="#" class="forgot-password">忘记密码？</a>
          </div>

          <el-form-item class="login-buttons">
            <el-button type="primary" class="login-btn" size="large" @click="submitForm(ruleFormRef)">
              登录
            </el-button>
            <el-button class="reset-btn" size="large" @click="resetForm(ruleFormRef)">
              重置
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref, onMounted } from 'vue'
import { useCounterStore } from '@/stores/counter'
const stores = useCounterStore();
import { ElMessage, type FormInstance, type FormRules } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'
import axios from 'axios'
import{read} from '@/Until/axiosHepler'
import { useRouter, useRoute } from 'vue-router';
const router = useRouter();

interface RuleForm {

  userName: string
  password: string
  regin: boolean
  savepassword: string
}

const ruleFormRef = ref<FormInstance>()
const ruleForm = reactive<RuleForm>({
  userName: "",
  password: "",
  regin: false,
  savepassword: ''


})
onMounted(() => {
  GetRember();
})
//记住密码
const GetRember = () => {
  const name = localStorage.getItem("name");
  const pwd = localStorage.getItem("pwd");
  if (name != null && pwd != null) {
    ruleForm.userName = name;
    ruleForm.password = pwd;
    ruleForm.regin = true;
  }
  else {
    ruleForm.regin = false;
  }
}
const locationOptions = ['Home', 'Company', 'School']

const rules = reactive<FormRules<RuleForm>>({
  userName: [
    { required: true, message: '用户名不能为空', trigger: 'blur' },

  ],
  password: [
    {
      required: true,
      message: '密码不能为空',
      trigger: 'blur',
    },
  ],

})

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {

      read.post("api/User/Login", ruleForm).then((res => {


        if (res.data.code == 200) {

          ElMessage.success("登录成功");

          stores.createApp.userName = res.data.data.userName;
          stores.createApp.nickName = res.data.data.nickName;
          stores.createApp.roleName = res.data.data.roleName;

          localStorage.setItem("name", ruleForm.userName);

          // alert(localStorage.getItem("name"))
          //   alert(from.regin)
          if (ruleForm.regin) {
            localStorage.setItem("name", ruleForm.userName);
            localStorage.setItem("pwd", ruleForm.password);

          }
          else {
            localStorage.removeItem("name");
            localStorage.removeItem("pwd");

          }
          stores.createApp.userName = res.data.data.userName
          stores.createApp.userId = res.data.data.userId
          stores.createApp.roleId = res.data.data.roleId
          stores.createApp.roleName = res.data.data.roleName
          stores.createApp.nickName = res.data.data.nickName
          stores.createApp.token = res.data.data.token
          router.push("/MainView")
          // 登录判定



        }
        else {
          ElMessage.success("登录失败");
        }
      }))
    } else {
      console.log('error submit!', fields)
    }
  })
}

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}

const options = Array.from({ length: 10000 }).map((_, idx) => ({
  value: `${idx + 1}`,
  label: `${idx + 1}`,
}))
</script>

<style scoped>
.login-page {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  overflow: hidden;
}

.background-decoration {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

.circle {
  position: absolute;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
  animation: float 6s ease-in-out infinite;
}

.circle-1 {
  width: 200px;
  height: 200px;
  top: 10%;
  left: 10%;
  animation-delay: 0s;
}

.circle-2 {
  width: 150px;
  height: 150px;
  top: 60%;
  right: 15%;
  animation-delay: 2s;
}

.circle-3 {
  width: 100px;
  height: 100px;
  bottom: 20%;
  left: 20%;
  animation-delay: 4s;
}

@keyframes float {

  0%,
  100% {
    transform: translateY(0px);
  }

  50% {
    transform: translateY(-20px);
  }
}

.login-container {
  width: 100%;
  max-width: 400px;
  padding: 20px;
  position: relative;
  z-index: 1;
}

.login-card {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(20px);
  border-radius: 20px;
  padding: 40px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
  position: relative;
  overflow: hidden;
}

.login-card::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 4px;
  background: linear-gradient(90deg, #667eea, #764ba2);
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.login-title {
  font-size: 28px;
  font-weight: 700;
  color: #2d3748;
  margin: 0 0 8px 0;
  background: linear-gradient(135deg, #667eea, #764ba2);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.login-subtitle {
  color: #718096;
  font-size: 14px;
  margin: 0;
}

.login-form {
  width: 100%;
}

.input-group {
  position: relative;
  margin-bottom: 20px;
}

.input-icon {
  position: absolute;
  left: 15px;
  top: 50%;
  transform: translateY(-50%);
  color: #a0aec0;
  z-index: 2;
  font-size: 18px;
}

.custom-input {
  width: 100%;
}

.custom-input :deep(.el-input__wrapper) {
  padding-left: 45px;
  border-radius: 12px;
  border: 2px solid #e2e8f0;
  background: #f7fafc;
  transition: all 0.3s ease;
  box-shadow: none;
}

.custom-input :deep(.el-input__wrapper:hover) {
  border-color: #667eea;
  background: #fff;
}

.custom-input :deep(.el-input__wrapper.is-focus) {
  border-color: #667eea;
  background: #fff;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

.custom-input :deep(.el-input__inner) {
  color: #2d3748;
  font-size: 16px;
}

.custom-input :deep(.el-input__inner::placeholder) {
  color: #a0aec0;
}

.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30px;
}

.remember-checkbox {
  color: #4a5568;
}

.remember-checkbox :deep(.el-checkbox__label) {
  color: #4a5568;
  font-size: 14px;
}

.forgot-password {
  color: #667eea;
  text-decoration: none;
  font-size: 14px;
  font-weight: 500;
  transition: color 0.3s ease;
}

.forgot-password:hover {
  color: #764ba2;
}

.login-buttons {
  margin-bottom: 0;
}

.login-btn {
  width: 100%;
  height: 50px;
  background: linear-gradient(135deg, #667eea, #764ba2);
  border: none;
  border-radius: 12px;
  font-size: 16px;
  font-weight: 600;
  margin-bottom: 15px;
  transition: all 0.3s ease;
  box-shadow: 0 4px 15px rgba(102, 126, 234, 0.4);
}

.login-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(102, 126, 234, 0.6);
}

.reset-btn {
  width: 100%;
  height: 50px;
  background: #f7fafc;
  border: 2px solid #e2e8f0;
  color: #4a5568;
  border-radius: 12px;
  font-size: 16px;
  font-weight: 600;
  transition: all 0.3s ease;
}

.reset-btn:hover {
  background: #edf2f7;
  border-color: #cbd5e0;
  transform: translateY(-1px);
}

/* 响应式设计 */
@media (max-width: 480px) {
  .login-container {
    padding: 15px;
  }

  .login-card {
    padding: 30px 20px;
  }

  .login-title {
    font-size: 24px;
  }

  .form-options {
    flex-direction: column;
    gap: 15px;
    align-items: flex-start;
  }
}
</style>
